from pattern.circuit import Control


class entry(Control):
	
	class User(object):
		pass
	
	def __init__(self):
		self.users = { }
		
	def realize(self, event):
		self.updated(event)
		
	def incoming(self, event):
		action, user = event.data
		if action == "enter":
			self.enter(event, user)
		elif action == "leave":
			self.leave(event, user)
			
	def enter(self, event, user):
		userobj = self.User(); userobj.wfile = event.trail.wfile
		self.users[user] = userobj
		self.updated(event)
	
	def leave(self, event, user):
		try:
			del self.users[user]
			self.updated(event)
		except KeyError:
			pass
		
	def updated(self, event):
		bridge = self.acquireInterface(event, "Users")
		if bridge is not None:
			bridge.push(self)
	
	def please(self, event):
		return self.users
		
	def __repr__(self):
		return str(self.users.keys())


